package com.itsitio.registeradmin.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.itsitio.registeradmin.dao.BranchDao;
import com.itsitio.registeradmin.dao.CompanyDao;
import com.itsitio.registeradmin.dao.ProvinceDao;
import com.itsitio.registeradmin.model.Branch;
import com.itsitio.registeradmin.model.Company;
import com.itsitio.registeradmin.model.Province;
import com.itsitio.registeradmin.service.BranchSrv;

@Service
public class BranchSrvImpl implements BranchSrv{

	@Autowired
	private BranchDao branchDao;

	@Autowired
	private CompanyDao companyDao;
	
	@Autowired
	private ProvinceDao provinceDao;

	public List<Branch> getBranchList() {
		return branchDao.obtenerTodos();
	}

	public List<Branch> getBranchListByCompany(Company company) {
		List<Branch> branchList = branchDao.getBranchListByCompany(company.getId());
		company.setBranches(branchList);
		return branchList;
	}

	@Override
	public void updateBranchWithUserRegistrationData(Integer selectedBranch,
			String branchAddress, String branchCity, Integer branchProvince,
			String zipCode, String branchPhone) {
		
		Branch branch = branchDao.obtener(selectedBranch);
		Province province = provinceDao.obtener(branchProvince);
		
		branch.setCity(branchCity);
		branch.setProvince(province);
		branch.setAddress(branchAddress);
		branch.setZipCode(zipCode);
		branch.setPhone(branchPhone);
		
		branchDao.guardar(branch);
		
		
	}

	@Override
	public Branch createBranchFromUserRegistration(Integer selectedCompany,
			String branchAddress, String branchCity, Integer branchProvince,
			String zipCode, String branchPhone) {
		
		Branch branch = new Branch();
		
		Company company = companyDao.obtener(selectedCompany);
		
		Province province = provinceDao.obtener(branchProvince);
		
		branch.setCity(branchCity);
		branch.setProvince(province);
		branch.setAddress(branchAddress);
		branch.setZipCode(zipCode);
		branch.setPhone(branchPhone);
		branch.setCompany(company);
		
		branchDao.guardar(branch);
		return branch;
	}

	@Override
	public Branch obtener(Integer branchId) {
		return branchDao.obtener(branchId);
	}
	



}
